package com.graduate.examapp.adapter

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.CheckBox
import android.widget.TextView
import android.widget.Toast
import android.widget.ToggleButton
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.switchmaterial.SwitchMaterial
import com.graduate.examapp.R
import com.graduate.examapp.entity.StatusParam
import com.graduate.examapp.entity.Teacher
import com.graduate.examapp.model.TeacherRepository
import com.graduate.examapp.viewmodel.TeacherViewModel

class TeacherRecycleViewAdapter(
    private val teacherViewModel: TeacherViewModel,
    private val context: Context
): RecyclerView.Adapter<TeacherRecycleViewAdapter.TaskItemHolder>() {
    var dataSource: MutableList<Teacher> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TaskItemHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.teacher_item, parent, false)
        return TaskItemHolder(view)
    }

    override fun onBindViewHolder(holder: TaskItemHolder, i: Int) {
        val examText = StringBuilder()
        val teacher = dataSource[i]
        examText.append("教师工号：")
            .append(teacher.id)
            .append("\n")
            .append("姓名：")
            .append(teacher.username)

        holder.teacherTextView.text = examText.toString()

        holder.toggle.isChecked = dataSource[i].isForbidden

        holder.delButton.setOnClickListener {
            //弹出确认框 是否删除
            AlertDialog.Builder(context)
                .setMessage("你确认要删除该教师吗？")
                .setPositiveButton("确认"
                ) { dialog, which ->
                    //发送请求 删除这个教师
                    val forbidden = if (dataSource[i].isForbidden) 1 else 0
                    val teacherDelParam = StatusParam(
                        dataSource[i].id,
                        dataSource[i].username,
                        forbidden
                    )
                    Log.d("TeacherRecycleViewAdapter", teacherDelParam.toString())
                    teacherViewModel.delTeacher(teacherDelParam, i)
                }.setNegativeButton("取消", null)
                .create()
                .show()

        }

        holder.toggle.setOnClickListener {
            //发送请求禁用教师
            val forbidden = if (holder.toggle.isChecked) 0 else 1
            val statusParam = StatusParam(
                dataSource[i].id,
                dataSource[i].username,
                forbidden
            )
            teacherViewModel.changeTeacherStatus(statusParam)
        }
    }

    override fun getItemCount(): Int {
        return dataSource.size
    }

    class TaskItemHolder(taskItemView: View) : RecyclerView.ViewHolder(taskItemView) {
        val teacherTextView = taskItemView.findViewById<TextView>(R.id.admin_teacher_tx)
        val delButton = taskItemView.findViewById<Button>(R.id.del_teacher)
        val toggle = taskItemView.findViewById<SwitchMaterial>(R.id.teacher_toggle)
    }
}